package cn.exrick.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.exrick.mapper.TbItemSkuGoodCatMapper;
import cn.exrick.pojo.TbItemSkuGoodCat;
import cn.exrick.pojo.TbItemSkuGoodCatAttr;
import cn.exrick.pojo.dto.ItemDto;
import cn.exrick.service.ItemService;
import cn.exrick.service.ItemSkuGoodCatAttrService;
import cn.exrick.service.ItemSkuGoodCatService;

@Service
public class ItemSkuGoodCatServiceImpl extends BaseServiceImpl<TbItemSkuGoodCat, TbItemSkuGoodCatMapper> implements ItemSkuGoodCatService {
    private final static Logger log = LoggerFactory.getLogger(ItemSkuGoodCatServiceImpl.class);

    @Override
    public Logger getLogger() {
        return log;
    }

    @Autowired
    TbItemSkuGoodCatMapper tbItemSkuGoodCatMapper;
    @Autowired
    ItemSkuGoodCatAttrService itemSkuGoodCatAttrService;
    @Autowired
    ItemService itemService;

    @Override
    public TbItemSkuGoodCatMapper getMapper() {
        return tbItemSkuGoodCatMapper;
    }

    @Override
    public TbItemSkuGoodCat findTopestCatByItemId(long itemId) {
        TbItemSkuGoodCatAttr itemSkuGoodCatAttr = itemSkuGoodCatAttrService.findByItemId(itemId);
        if(itemSkuGoodCatAttr == null){
            return null;
        }
        TbItemSkuGoodCat last = findById(itemSkuGoodCatAttr.getItemSkuGoodCatId());
        while (last.getFatherItemSkuGoodCatId() != null) {
            TbItemSkuGoodCatAttr father = itemSkuGoodCatAttrService.findById(last.getFatherItemSkuGoodCatId());
            last = findById(father.getItemSkuGoodCatId());
        }
        return last;
    }

    /**
     * 通过商品id查询sku树
     * @param itemId
     * @return
     */
    public TbItemSkuGoodCat findTreeByItemId(long itemId){
        TbItemSkuGoodCat top = findTopestCatByItemId(itemId);
        if(top == null){
            return null;
        }
        return findTreeByTopId(top.getId());
    }

    /**
     * 通过cat查询这个起点的树
     * @param catId
     * @return
     */
    public TbItemSkuGoodCat findTreeByTopId(long catId){
        TbItemSkuGoodCat cat = findById(catId);
        List<TbItemSkuGoodCatAttr> attrs = itemSkuGoodCatAttrService.findByCatId(catId);
        for(TbItemSkuGoodCatAttr attr:attrs){
            TbItemSkuGoodCat next = findCatByAttrId(attr.getId());
            if(next != null){
                next = findTreeByTopId(next.getId());
                attr.setNextCat(next);
            }
        }
        cat.setAttrs(attrs);
        return cat;
    }



    public List<ItemDto> findBrotherItem(long itemId) {
        TbItemSkuGoodCat top = findTopestCatByItemId(itemId);
        if(top == null){
            return null;
        }
        return findItemByCatId(top.getId());
    }

    public List<ItemDto> findItemByCatId(long catId) {
        List<ItemDto> result = new ArrayList<>();
        List<TbItemSkuGoodCatAttr> attrs = itemSkuGoodCatAttrService.findByCatId(catId);
        for (TbItemSkuGoodCatAttr attr : attrs) {
            if (attr.getItemId() != null) {
                ItemDto itemDto = itemService.getItemById(attr.getItemId());
                if (itemDto != null) {
                    result.add(itemDto);
                }
            } else {
                TbItemSkuGoodCat child = findCatByAttrId(attr.getId());
                result.addAll(findItemByCatId(child.getId()));
            }
        }
        return result;
    }

    /**
     * 通过attrId查询他的下级cat
     *
     * @param attrId
     * @return
     */
    public TbItemSkuGoodCat findCatByAttrId(long attrId) {
        return findByUniqueForOne("father_item_sku_good_cat_id", String.valueOf(attrId));
    }

    @Override
    public TbItemSkuGoodCatAttr findFather(long catId) {
        TbItemSkuGoodCat cat = findById(catId);
        if (cat == null) {
            return null;
        }
        return itemSkuGoodCatAttrService.findById(cat.getFatherItemSkuGoodCatId());
    }

}
